package hn.sigit.view;

import hn.sigit.logic.general.GeneralHelperX;
import hn.sigit.logic.general.ResourceBundleHelper;
import hn.sigit.logic.security.Authenticator;
import hn.sigit.model.hnd.administrative.HND_User;
import hn.sigit.dao.hnd.administrative.HND_UserDAO;

import java.util.List;
import java.io.Serializable;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;

@Name("usersHelper")
@Scope(value = ScopeType.CONVERSATION)
@AutoCreate
public class UsersHelper implements Serializable {
	private static final long serialVersionUID = 1L;

	private List<HND_User> users;
	private String password;
	private String confPassword;
	private HND_User selected;
	private int currentRow;
	
	
	private String newUserPartyName;
	
	@In
	private ResourceBundleHelper resBundle;
	
	@In
	private GeneralHelperX generalHelper;

	
	public List<HND_User> getUsers() {
		if (users == null)
			users = HND_UserDAO.loadUsers();
		
		return users;
	}
	public void setUsers(List<HND_User> users) {
		this.users = users;
	}
	
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	public String getConfPassword() {
		return confPassword;
	}
	public void setConfPassword(String confPassword) {
		this.confPassword = confPassword;
	}
	public HND_User getSelected() {
		return selected;
	}
	public void setSelected(HND_User selected) {
		this.selected = selected;
	}
	
	public int getCurrentRow() {
		return currentRow;
	}
	public void setCurrentRow(int currentRow) {
		this.currentRow = currentRow;
	}
	
	public String getNewUserPartyName() {
		return newUserPartyName;
	}
	public void setNewUserPartyName(String newUserPartyName) {
		this.newUserPartyName = newUserPartyName;
	}
	
	public String newUser() {
		setSelected(new HND_User());
		setNewUserPartyName("");
		
		return null;
	}
	
	public String editUser(HND_User user) {
		setSelected(user);
		
		return null;
	}
	
	public String toggleActivation(HND_User user) {
		user.setActive(!user.getActive());
		HND_UserDAO.save(user);
		
		return null;
	}
	
	public String applyUserDataChange() {
		if (selected != null)
			HND_UserDAO.save(selected);

		return null;
	}
	
	public String saveSelected() {
		if (selected != null) {
			if (password.equals(confPassword)) {
				selected.setPassword(Authenticator.MD5(password));
				if (selected.getORMID() == null)
					setUsers(null);
				HND_UserDAO.save(selected);
			}
			else {
				FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR,
						getResBundle().loadMessage("user.reset_password_err"),
						getResBundle().loadMessage("user.reset_password_err_detail"));
				FacesContext.getCurrentInstance().addMessage(null, message);
			}
		}
		
		return null;
	}


	protected ResourceBundleHelper getResBundle() {
		if (resBundle == null) {
			resBundle = generalHelper.getResBundle();
		}
		return resBundle;
	}
}
